﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using YTSK.Vouch;

namespace YTSK.Frm
{
    public class FrmKPFPDisplayBase : VouchToolBar
    {
        #region 初始化
        public FrmKPFPDisplayBase(string cvouchtype, Control c, ToolStrip toolstrip, string cdefine2) :
            base(cvouchtype, c, toolstrip, cdefine2)
        {
        }
        #endregion



        private List<Dictionary<string, object>> GetDisplayData()
        {
            var list = new List<Dictionary<string, object>>();
            var vdetail = base._vouchs.Where(x => x.iHead == 2 && x.FieldCode != "dataGrid").ToList();
            for (int i = 0; i < base._dg.Rows.Count; i++)
            {
                var dgr = base._dg.Rows[i];
                var dic = new Dictionary<string, object>();
                for (int j = 0; j < base._dg.Columns.Count; j++)
                {
                    var col = base._dg.Columns[j];
                    dic.Add(col.Name, dgr.Cells[j].Value);
                }
                list.Add(dic);
            }
            return list;
        }


        #region 修改
        public void Modify()
        {
            try
            {
                var url = string.Format("{0}/api/oms/SKSalebillVouch/Modify?token={1}&id={2}",
                        ServerConfig.ServerAddress, ServerConfig.GetToken(), _id);
                var d = new Dictionary<string, object>();
                d.Add("ID", _id); d.Add("ts", _ts);
                var s = Common.handleHttp.Post(d, url);
                var result = Common.handleJSON.DeserializeResult(s);
                if (result.result != 1) { MessageBox.Show("出错:" + result.resultdetail); return; }

                #region 显示数据
                var customer = (Dictionary<string, object>)result.GetVal("Customer");
                var rd = (Dictionary<string, object>)result.GetVal("rdrecord");
                _id = rd["ID"].ObjToInt();
                _ts = rd["ts"].ObjToString();
                var rds = result.GetListDic("rdrecords");
                base.DisplayDataForm(customer, rd);
                base.Displaydata(rds, true);
                SetToolBarEnable(false);
                _bVouchModifyStatus = true;
                #endregion
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + ex.StackTrace);
            }
        }
        #endregion

        #region 保存单据
        public void SaveVouch()
        {
            try
            {
                string err;
                List<Dictionary<string, object>> l_detail;
                var main = GetVouchData(out err, out l_detail);
                if (err.Length > 0) { MessageBox.Show("数据校验出现错误：" + err); return; }
                var post = new Dictionary<string, object>();
                post.Add("main", main);
                post.Add("detail", l_detail);
                var poststr = Common.handleJSON.Serialize(post);

                var url = string.Format("{0}/api/oms/SKSalebillVouch/UpdateTry?token={1}",
                      ServerConfig.ServerAddress, ServerConfig.GetToken());
                var s = Common.handleHttp.Post(poststr, url);
                var result = Common.handleJSON.DeserializeResult(s);
                if (result.result == 1001)
                {
                    var dr = MessageBox.Show(result.resultdetail, "警告", MessageBoxButtons.YesNo);
                    if (dr == DialogResult.No)
                    { return; }
                }
                else
                {
                    if (result.result != 1) { MessageBox.Show(result.resultdetail); }
                }
                url = string.Format("{0}/api/oms/SKSalebillVouch/Update?token={1}",
                      ServerConfig.ServerAddress, ServerConfig.GetToken());
                s = Common.handleHttp.Post(poststr, url);
                result = Common.handleJSON.DeserializeResult(s);
                if (result.result != 1) { MessageBox.Show("保存出错:" + result.resultdetail); return; }

                #region 显示数据
                var customer = (Dictionary<string, object>)result.GetVal("Customer");
                var rd = (Dictionary<string, object>)result.GetVal("rdrecord");
                _id = rd["ID"].ObjToInt();
                _ts = rd["ts"].ObjToString();
                var rds = result.GetListDic("rdrecords");
                base.DisplayDataForm(customer, rd);
                base.Displaydata(rds, true);
                SetToolBarEnable(true);
                _bVouchModifyStatus = false;
                #endregion
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + ex.StackTrace);
            }

        }
        #endregion

        #region 放弃
        public void Cancel()
        {
            try
            {
                if (_id == 0)
                {
                    var customer = new Dictionary<string, object>();
                    var rd = new Dictionary<string, object>();
                    _id = 0;
                    _ts = "";
                    var rds = new List<Dictionary<string, object>>();
                    base.DisplayDataForm(customer, rd);
                    base.Displaydata(rds, true);
                    SetToolBarEnable(true);
                    _bVouchModifyStatus = false;
                }
                else { Reflesh(_id); }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + ex.StackTrace);
            }
        }
        #endregion

        #region 显示数据
        public void Reflesh(int id)
        {
            try
            {
                _id = id;
                var url = string.Format("{0}/api/oms/SKSalebillVouch/GetOne?token={1}&ID={2}",
                        ServerConfig.ServerAddress, ServerConfig.GetToken(), _id);
                var s = Common.handleHttp.Get(url);
                var result = Common.handleJSON.DeserializeResult(s);
                if (result.result != 1) { MessageBox.Show("出错:" + result.resultdetail); return; }

                #region 显示数据
                var customer = (Dictionary<string, object>)result.GetVal("Customer");
                var rd = (Dictionary<string, object>)result.GetVal("rdrecord");
                _id = rd["ID"].ObjToInt();
                _ts = rd["ts"].ObjToString();
                var rds = result.GetListDic("rdrecords");
                base.DisplayDataForm(customer, rd);
                base.Displaydata(rds, true);
                SetToolBarEnable(true);
                _bVouchModifyStatus = false;
                #endregion
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + ex.StackTrace);
            }
        }
        #endregion
    }
}
